//
//  WifeViewController.m
//  CarHome
//
//  Created by apple on 15/3/21.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "WifeViewController.h"
<<<<<<< HEAD
#import "AFNetworking.h"
#import "WifeData.h"
#import "MMDrawerBarButtonItem.h"
#import "MMDrawerController.h"
#import "UIViewController+MMDrawerController.h"
#import "MMDrawerVisualState.h"
#import "MJRefresh.h"



#import "ACollectionViewCell.h"
#import "WifeTableViewCell.h"
#import "WebViewController.h"


@interface WifeViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>




@property (nonatomic, retain) NSMutableArray *arr;
=======
#import "WifeTableViewCell.h"
#import "AFNetworking.h"
#import "WifeData.h"
#import "LeftViewController.h"
#import "MMDrawerBarButtonItem.h"
#import "MMDrawerController.h"
#import "UIViewController+MMDrawerController.h"

@interface WifeViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UISearchBar *searchBar;


>>>>>>> 8bf931e2f3780ca24983ee1bdd6c2838e568d2ac



@end


@implementation WifeViewController

- (void)dealloc
{
<<<<<<< HEAD
    [_aaid release];
    [_arr release];
    [_data release];
    [_collect release];
    [_dataArr release];
    [_buttonArr release];
    [_scroll release];
    [_bodyArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.navigationItem setHidesBackButton:YES];
        [self.view setBackgroundColor:[UIColor redColor]];
        self.navigationController.navigationBar.translucent = NO;
        
        self.navigationItem.title = @"全部";
        self.bodyArr = [NSMutableArray array];
        
        [_bodyArr addObject:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c0-p1-s30.html"];// 全部
        [_bodyArr addObject:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c104-p1-s30.html"];// 媳妇当车模
        [_bodyArr addObject:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c110-p1-s30.html"]; // 美人计
        [_bodyArr addObject:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c172-p1-s30.html"]; // 论坛名人堂
// 通知中心
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotification:) name:@"aa" object:nil];
        
        
        
    }
    return self;
}



=======
    [_searchBar release];
    [_data release];
    [_tableView release];
    [_arr release];
    [super dealloc];
}

>>>>>>> 8bf931e2f3780ca24983ee1bdd6c2838e568d2ac
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"媳妇当车模";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
<<<<<<< HEAD
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-sousuo.png"] style:UIBarButtonItemStylePlain target:self action:@selector(lookfor:)];
    
    
    self.buttonArr = [NSMutableArray array];
    self.dataArr = [NSMutableArray array];
    [_dataArr addObject:@"大杂烩"];
    [_dataArr addObject:@"媳妇当车模"];
    [_dataArr addObject:@"美人计"];
    [_dataArr addObject:@"论坛名人堂"];
   
    
    [self creatcollection];
    [self createScroll];
    
    
}
#pragma mark - 搜索
- (void)lookfor:(UIBarButtonItem *)item
{
    
}

#pragma mark - 通知中心跳转页面

- (void)receiveNotification:(NSNotification *)noti
{
    
    WebViewController *web = [[WebViewController alloc] init];
    web.wriid = noti.object;
    [self.navigationController pushViewController:web animated:YES];
    [web release];
}


#pragma mark - 创建collectionview
- (void)creatcollection
{
    UICollectionViewFlowLayout *flo = [[UICollectionViewFlowLayout alloc] init];
    [flo setItemSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
    [flo setSectionInset:UIEdgeInsetsMake(0, 5, 0, 0)];
    [flo setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 25, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flo];
    [flo setMinimumLineSpacing:0];//设置item间距为0
    [_collect setBackgroundColor:[UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1]];
    _collect.delegate = self;
    _collect.dataSource = self;
    [_collect setPagingEnabled:YES];
    
    [_collect registerClass:[ACollectionViewCell class] forCellWithReuseIdentifier:@"a"];    

    //设置scroll的可偏移范围，根据多少个页面
    [_collect setContentSize:CGSizeMake(self.view.frame.size.width * [_dataArr count], 0)];
    [self.view addSubview:_collect];
    [_collect release];
    
    
}

#pragma mark - 顶端scroll以及button的创建
- (void)createScroll
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, 50)];
    _scroll.contentSize = CGSizeMake(800, 0);
    _scroll.delegate = self;
    _scroll.showsHorizontalScrollIndicator = NO;
    [_scroll setBackgroundColor:[UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1]];
    [self.view addSubview:_scroll];
    int ConWidth = 0; // 偏移量
    int button_x = 0;
    for (int i = 0; i < _dataArr.count; i++) {
        int buttonWidth = 60;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.layer.cornerRadius = 10;
        button.tag = 20 + i;
        [_buttonArr addObject:button];
        if ([[_dataArr objectAtIndex:i]length] == 5) {
            buttonWidth = 90;
            button.frame = CGRectMake(button_x + 10, 10, buttonWidth, 30);
            button_x = button.frame.origin.x + buttonWidth;
        }else
        {
            buttonWidth = 70;
            button.frame = CGRectMake(button_x + 10, 10, buttonWidth, 30);
            button_x = button.frame.origin.x + buttonWidth;
        }
        // 设置button颜色
        if (i == 0) {
            button.backgroundColor = [UIColor blueColor];
            
        }else
        {
            button.backgroundColor = [UIColor whiteColor];
            
        }
        [button setTitle:[_dataArr objectAtIndex:i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [self.scroll addSubview:button];
        ConWidth = button.frame.origin.x + 100;
    }
    _scroll.contentSize = CGSizeMake(ConWidth, 0);
    [_scroll release];
    
    
}
#pragma mark - 根据页面偏移量改变button位子
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}




#pragma mark - 点击button

- (void)changeColor:(UIButton *)button
{
    [_collect setContentOffset:CGPointMake((button.tag - 20) * self.view.frame.size.width, -64) animated:YES];
}




#pragma mark - collectionview协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%ld", self.dataArr.count);
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        ACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"a" forIndexPath:indexPath];
        cell.urlBody = [_bodyArr objectAtIndex:0];
        return cell;
    }if (indexPath.item == 1) {
        ACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"a" forIndexPath:indexPath];
        cell.urlBody = [_bodyArr objectAtIndex:1];
        return cell;
    }if (indexPath.item == 2) {
        ACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"a" forIndexPath:indexPath];
        cell.urlBody = [_bodyArr objectAtIndex:2];
        return cell;
    }
    ACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"a" forIndexPath:indexPath];
    cell.urlBody = [_bodyArr objectAtIndex:3];
    return cell;
   
=======
    self.arr = [NSMutableArray array];
    
    [self path];
    
//     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-zhengfuxinxigongkaimulu.png"] style:UIBarButtonItemStylePlain target:self action:@selector(AddButton:)];
    
    
    MMDrawerBarButtonItem *dra = [[MMDrawerBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-zhengfuxinxigongkaimulu.png"] style:UIBarButtonItemStylePlain target:self action:@selector(AddButton:)];
    dra.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = dra;
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.searchBar = [[UISearchBar alloc] init];
    self.searchBar.frame = CGRectMake(300, 300, 50, 35);
    self.tableView.tableHeaderView = self.searchBar;
    
    [self.tableView registerClass:[WifeTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    
    
    
    
    
}
#pragma mark - 侧边栏按钮
- (void)AddButton:(id)sender
{
//    LeftViewController *leftView = [[LeftViewController alloc] init];
//    [self.navigationController pushViewController:leftView animated:YES];
//    [leftView release];
    [self.mm_drawerController openDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

#pragma mark - 数据源

- (void)path
{
    
    NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c104-p1-s30.html"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
        id dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"获取到数据为: %@", dict);
        
        NSDictionary *dic = dict;
        NSDictionary *dicc = [dic valueForKey:@"result"];
        NSArray *arr = [dicc valueForKey:@"list"];
        for (NSDictionary *dic in arr) {
            WifeData *data = [[WifeData alloc] initWithDictionary:dic];
            [self.arr addObject:data];
            [data release];
        }
        
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"出现错误: %@", error);
        
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    
}
#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@".........%ld", self.arr.count);
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WifeTableViewCell *cell = [[WifeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    WifeData *data = [self.arr objectAtIndex:indexPath.row];
    cell.data = data;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark - searchdelegate

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self.arr removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];//用于过滤
    self.arr = [NSMutableArray arrayWithArray:[self.arr filteredArrayUsingPredicate:predicate]];
    return  YES;
}

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    //当scope改变时调用
    return YES;
>>>>>>> 8bf931e2f3780ca24983ee1bdd6c2838e568d2ac
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
